package hjt.com.module_test.widget;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.Toast;

import androidx.annotation.Nullable;

public class DogView extends BaseView {

    private Paint dogPaint;
    private Rect rect = new Rect();
    private int vWidth;
    private int vHeight;
    private ValueAnimator valueAnimator;
    private float animatedValue;
    private PointF pointF;
    private Paint pointPaint;

    public DogView(Context context) {
        this(context, null);
    }

    public DogView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DogView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        dogPaint = new Paint();
        dogPaint.setColor(Color.RED);

        pointPaint = new Paint();
        pointPaint.setColor(Color.GREEN);

        pointF = new PointF();

        valueAnimator = new ValueAnimator();
        valueAnimator.setDuration(2000);
        valueAnimator.setRepeatCount(-1);
        valueAnimator.setFloatValues(0);
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                animatedValue = (float) animation.getAnimatedValue();
                invalidate();
            }
        });
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        vWidth = w;
        vHeight = h;
        rect.left = 0;
        rect.right = 100;
        rect.top = -100;
        rect.bottom = 0;
        valueAnimator.setFloatValues(0, vWidth, 0);

        pointF.x = vWidth / 2;
        pointF.y = vHeight / 3;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(getSize(widthMeasureSpec, getResources().getDisplayMetrics().widthPixels), getSize(heightMeasureSpec, getResources()
                .getDisplayMetrics().heightPixels));
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        start();
    }

    private void start() {
        if (!valueAnimator.isRunning()) {
            valueAnimator.start();
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.save();
        canvas.translate(0, vHeight / 2);
        canvas.drawLine(0, 0, vWidth, 0, dogPaint);
        canvas.drawRect(rect.left + animatedValue, rect.top, rect.right + animatedValue, rect.bottom, dogPaint);

        canvas.restore();
        canvas.drawCircle(pointF.x, pointF.y, 20, pointPaint);

        if (isInner()) {
            Toast.makeText(getContext(), pointF.x + ":" + pointF.y, Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        pointF.x = event.getX();
        pointF.y = event.getY();
        return true;
    }

    private boolean isInner() {
        if (pointF.x >= animatedValue && pointF.x <= (rect.right + animatedValue)) {
            if (pointF.y >= (vHeight / 2 + rect.top) && pointF.y <= (vHeight / 2)) {
                return true;
            }
        }
        return false;
    }
}
